<--- %%NOBANNER%% --> nobs.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Count the total number of observations;                            |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| 1st argument: indata = or data = or just a data name;              |
| 2nd argument: return=T / Output - return the number of             |
|                    observations as an integer;                     |
|               return=F / LOG - write the number of observations    |
|                    to the log window;                              |
|               return=  / Both - return the number and write to     |
|                    the log window;                                 |
|---------------<-- End of Files Arguments Needed-->-----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| example: %nobs(one, return=log);/%nobs(one, return=F);             |
|          %nobs(indata=one, return=output); /                       |
|          %nobs(indata=one, return=T);                              |
|          %nobs(data=two);/%nobs(two);/%nobs(one, return=both);     |
\-------------------<-- End of Files Created-->---------------------*/
%macro nobs/parmbuff;
/*--------------------------------------------\
| Author:  Duo Zhou;                          |
| Created: 2-27-2001 11:23pm;                 |
| Purpose: Count the number of observations in|
|          a dataset;                         |
\--------------------------------------------*/
%local _nobsdataname_ _nobsnobs_ return;
%if (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),RETURN=)) %then %do;
   %let return=%qscan(&syspbuff,2,%str(,())); 
   %let return=%qscan(&return,2,%str(=)); 
%end;
%else %if (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=))) 
          or (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(OUTPUT=))) %then %do;%end;
%else %let return=OUTPUT;
%let _nobsdataname_=%qscan(&syspbuff,1,%str(,()));
%if (%index(%quote(%upcase(%sysfunc(compress(%quote(&_nobsdataname_))))),INDATA=)) or 
    (%index(%quote(%upcase(%sysfunc(compress(%quote(&_nobsdataname_))))),DATA=)) %then %do;
   %let _nobsdataname_=%qscan(&_nobsdataname_,2,%str(=));
%end;
%let _nobsdsid_=%sysfunc(open(&_nobsdataname_));
%if &_nobsdsid_ %then %do;
   %let any=%sysfunc (attrn(&_nobsdsid_, ANY));
   %if any>=1 %then %do;
      %let _nobsnobs_=%sysfunc(attrn(&_nobsdsid_,NOBS));
      %let _nobsrcs_=%sysfunc(close(&_nobsdsid_)); 
      %if (not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F))) and %index(%upcase(&return),LOG)) 
          or (not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F))) and %index(%upcase(&return),F)) 
          or (%index(%upcase(&return),BOTH))
         %then %do; %put --> Note: There are &_nobsnobs_ observations in dataset %data(&_nobsdataname_).;
      %end;
   %end;
   %else %do;
      %let _nobsnobs_=0;
      %if (not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F))) and %index(%upcase(&return),LOG)) 
          or (not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F))) and %index(%upcase(&return),F)) 
          or (%index(%upcase(&return),BOTH))
         %then %do; %put ==> Alert! There is no observation in the data set %data(&_nobsdataname_).;
      %end;
   %end;
%end;
%else %do;
   %let _nobsnobs_=.;
   %if (not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F))) and %index(%upcase(&return),LOG)) 
       or (not (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F))) and %index(%upcase(&return),F)) 
       or (%index(%upcase(&return),BOTH))
      %then %do; %put ==> Alert! Open for data set %data(&_nobsdataname_) failed.;
   %end;
%end;
%if (%index(%upcase(&return),T))
    or (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(OUTPUT=T)))
    or (%index(%quote(%upcase(%sysfunc(compress(%quote(&syspbuff))))),%str(LOG=F)))
    or (%index(%upcase(&return),OUTPUT)) or (%index(%upcase(&return),BOTH)) 
   %then %do; &_nobsnobs_
%end;
%mend nobs;